/***********************************************************************
 * $Id:: application.h 3871 2010-07-16 11:50:22Z gerritdewaard         $
 *
 * Project: LPC1100 BLDC Motor Control AN  
 *
 * Description:
 *			
 *     
 *
 ***********************************************************************
 * Software that is described herein is for illustrative purposes only
 * which provides customers with programming information regarding the
 * products. This software is supplied "AS IS" without any warranties.
 * NXP Semiconductors assumes no responsibility or liability for the
 * use of the software, conveys no license or title under any patent,
 * copyright, or mask work right to the product. NXP Semiconductors
 * reserves the right to make changes in the software without
 * notification. NXP Semiconductors also make no representation or
 * warranty that such application will be suitable for the specified
 * use without further testing or modification.
 **********************************************************************/

#ifndef __APPLICATION_H 
#define __APPLICATION_H

//#include <stdio.h>
//#include <string.h>

/* Chip level includes */
#include <LPC11xx.h>

#include "driver_config.h"
#include "target_config.h"
#include "core_cm0.h"

#include "gpio.h"
#include "clkconfig.h"
#include "timer16.h"
#include "timer32.h"
#include "uart.h"
#include "adc.h"
//#include "i2c.h"
//#include "debug_printf.h"
//#include "small_printf.h"
//#include "small_utils.h"
//#include "rom_drivers.h"

/* Board level includes */
//#include "oled.h"
//#include "joystick.h"
//#include "lm75a.h"

/* Application level includes */
#include "../src/BLDC.h"
#include "../src/regulator.h"
#include "../src/PID.h"
//#include "gui.h"
//#include "comms.h"


//#include "nxp_image.h"
//#include "ea_image.h"

//#define __LPC111x
//#define __LPC11C2x

/****************************************************
	SOFTWARE VERSION STRING (max 16 chars/row, max 1 rows)
*****************************************************/
#define SW_VERSION_STRING "MOJ PID demo appl.\n20110507 rev A"

/****************************************************  
	SYSTEM PINOUT 
*****************************************************/

/****************************************************/
/* System setup										*/
/* Please see also driver_config.h for driver inits */
/****************************************************/

#define USE_UART 			1		/* Enable (1)/ Disable (0) UART */
#define USE_NXP_GUI 		0		/* Enable the use of the NXP MC GUI */
#define USE_STARTUP_GUI 	1		/* Enable the startup graphics */

/* LPCXpresso Motor control peripherals */
#define MC_BOARD_ENABLE_OLED 			1
#define MC_BOARD_ENABLE_JOYSTICK		1
#define MC_BOARD_ENABLE_LM75			1
#define MC_BOARD_ENABLE_EEPROM			0


/****************************************************/
/* Systick definitions								*/
/****************************************************/
#define SysTick_VALUE		SystemCoreClock / 1000

/****************************************************/
/* Generic definitions 								*/
/****************************************************/

#ifndef NULL
#define NULL    ((void *)0)
#endif

#ifndef FALSE
#define FALSE   (0)
#endif

#ifndef TRUE
#define TRUE    (1)
#endif

/****************************************************/
/* TIMER related macros								*/
/****************************************************/
#define EMC0	4
#define EMC1	6
#define EMC2	8
#define EMC3	10

#define TIMER1	1
#define TIMER0	0

#define CAP_ENABLE 1
#define CAP_DISABLE 0
#define CAP_RISING 1
#define CAP_FALLING 2
#define CAP_EVENT 4

#define MATCH0	(1<<0)
#define MATCH1	(1<<1)
#define MATCH2	(1<<2)
#define MATCH3	(1<<3)

#define TEST_TIMER_NUM		1		/* 0 or 1 for 16-bit timers only */

/****************************************************/
/* BUTTON definitions  								*/
/****************************************************
	PIO0_4		BUT1		STOP
	PIO0_5		BUT2		START
	PIO0_6		BUT3		UP
	PIO0_7		BUT4		DOWN
*****************************************************/
#define BTN_STOP_PORT 	0
#define BTN_STOP_PIN  	4
#define BTN_START_PORT 	0
#define BTN_START_PIN  	5
#define BTN_UP_PORT 	0
#define BTN_UP_PIN  	6
#define BTN_DOWN_PORT 	0
#define BTN_DOWN_PIN  	7

//#define putchar(c) outbyte(c)


typedef volatile struct _SYNC_STRUCT // 20 bytes
{
	uint8_t 	syncVAL[4];				// 0:	sync bytes
	uint32_t	deviceID[2];			// 4: 	double DWORD DeviceID
	uint8_t 	MCtype;					// 12:  motor control type
	uint8_t 	RESERVED0;				// 13:
	uint16_t 	DataStructSize;			// 14:	Size of data structure
	uint16_t 	ScopeBufSize;			// 16: 	Size of scope buffer
	uint16_t 	RESERVED1;				// 18:
}SYNC_STRUCT;

typedef volatile struct _BLDC_STRUCT {
	volatile uint32_t RPM;
	volatile uint32_t SP;
}BLDC_STRUCT;

#define CMD_FOCEN 		'0'		// New value for FOC.enabled
#define CMD_SPDCEN 		'1'		// New value for FOC.speedcontrolenabled
#define CMD_MODE 		'2'		// New value for FOC.mode
#define CMD_QSP 		'3'		// New value for FOC.Qsp
#define CMD_DSP 		'4'		// New value for FOC.Dsp
#define CMD_DKP 		'5'		// New value for FOC.Dkp
#define CMD_DKI 		'6'		// New value for FOC.Dki
#define CMD_QKP 		'7'		// New value for FOC.Qkp
#define CMD_QKI 		'8'		// New value for FOC.Qki
#define CMD_SPSP 		'9'		// New value for FOC.speedSP
#define CMD_SPKP 		'a'		// New value for FOC.speedKp
#define CMD_SPKI 		'b'		// New value for FOC.speedKi
#define CMD_SPKD 		'c'		// New value for FOC.speedKd
#define CMD_FSMO 		'd'		// New value for FOC.F
#define CMD_GSMO 		'e'		// New value for FOC.G
#define CMD_KSLIDE 		'f'		// New value for FOC.Kslide
#define CMD_MAXERR 		'g'		// New value for FOC.MaxErr
#define CMD_OMFLTRC 	'h'		// New value for FOC.OmegaFilterC
#define CMD_CURROFF 	'i'		// New value for FOC.CurrentSampleOffset
#define CMD_ALIVE_FEED  'j'		// Connection alive feed
#define CMD_TEST2 		'k'		// New value for FOC.test2
#define CMD_TEST3 		'l'		// New value for FOC.test3
#define CMD_TEST4 		'm'		// New value for FOC.test4
#define CMD_MATCH		'n'		// New value for match activate and deactivate registers
#define CMD_SETTXLIST	'o'		// Set txlist items
#define CMD_SETTRIGGER	'p'		// Set trigger
#define CMD_EMPTY1		'q'
#define CMD_EMPTY2		'r'

#define CONN_TIMEOUT_VALUE	15 			// 1.5 sec. Gets a feed from GUI every sec.


#endif /* end __APPLICATION_H */

/*****************************************************************************
**                            End Of File
******************************************************************************/
